/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-parts
*
* @File name : PartService.java
*
* @Author : zhangfm
*
* @Date : 2017年4月24日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年4月11日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.parts.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import org.marketing.common.Response;
import com.yonyou.gmmc.parts.contants.PartsConstant;
import com.yonyou.gmmc.parts.dto.PartDto;
import com.yonyou.gmmc.parts.service.PartService;
import com.yonyou.gmmc.parts.util.ImportExcel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator TODO description
 * @date 2017年4月21日
 */
@RestController
@RequestMapping(value = PartsConstant.BASE_PATH + "/" + PartsConstant.VERSION)
@Api("纯正零部件api")
public class PartController {
	private static final Logger logger = Logger.getLogger(PartController.class);
	
	@Autowired
	private PartService partService;

    /**
    * 单个纯正零部件详情查詢
    * @author zhangfm
    * @date 2017年4月24日
    * @return
    */
	/*
	@RequestMapping(value = "/queryPartDetails", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public @ResponseBody List<PartDto> queryPartDetails(@Param("partId") String partId) {
		List<PartDto> dtoList = partService.queryPartDetails(partId);
		return dtoList;
	}
	*/
	
    /**
    * 零部件列表查詢
    * @author zhangfm
    * @date 2017年4月24日
    * @return
    */
	@RequestMapping(value = "/queryPartList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="零部件列表查詢",notes="零部件列表查詢")
	public  Response queryPartList(HttpServletRequest request,
			@RequestParam(required=false,defaultValue="1") Integer pageNum
     		,@RequestParam(required=false,defaultValue="img") String sort
     		,@RequestParam(required=false,defaultValue="desc") String sortType
     		,@RequestParam(required=false,defaultValue="10") String pageSize) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pageNum", pageNum);
	    	paramMap.put("pageSize", pageSize);
	    	paramMap.put("sort", sort);
	    	paramMap.put("sortType", sortType);
	    	PageInfo<PartDto> retMap = partService.queryPartList(paramMap);
			return new Response<PageInfo<PartDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
		} catch (Exception e){
			logger.error("查询错误",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
		
	}
	
    /**
    * 零部件按车系查詢
    * @author zhangfm
    * @date 2017年4月21日
    * @return
    */
	@RequestMapping(value = "/queryPartBySeries", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="零部件按车系查詢",notes="零部件按车系查詢")
	public Response queryPartBySeries(@Param("carSeriesId") String carSeriesId) {
		try {
			Map<String,Object> retMap = partService.queryPartBySeries(carSeriesId);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
		} catch (Exception e) {
			logger.error("查询错误",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
		
	}
	
	/**
	 * 品牌，种类查找
	 * author zhangfm
	 * date 2017年5月19日
	 */
	@RequestMapping(value = "/queryBrand", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="品牌，种类查找",notes="品牌，种类查找")
	public Response  queryBrand() {
		try {
			Map<String,Object> retMap = partService.queryBrand();
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
		} catch (Exception e){
			logger.error("查询错误",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
		
	}
	
	/**
	 * 按零件号或零件名查找
	 * author zhangfm
	 * date 2017年5月19日
	 */
	@RequestMapping(value = "/partByName", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="按零件号或零件名查找",notes="按零件号或零件名查找")
	public Response partByName(@Param("partName") String partName) {
		try {
			Map<String,Object> retMap = partService.partByName(partName);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
		} catch (Exception e){
			logger.error("查询错误",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}
	
	/**
	 * 按品牌和零件类型查找零部件
	 * author zhangfm
	 * date 2017年5月19日
	 */
	@RequestMapping(value = "/queryByIdType", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="按品牌和零件类型查找零部件",notes="按品牌和零件类型查找零部件")
	public Response queryByIdType(@Param("brand") String brand,@Param("type") String type) {
		try {
			Map<String,Object> retMap = partService.queryByIdType(brand,type);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
		} catch (Exception e){
			logger.error("查询错误",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}
	
    /**
    * 导入excel到数据库
    * @author zhangfm
    * @date 2017年4月27日
    * @return
    * @throws IOException 
    * @throws InvalidFormatException 
    */	
	@RequestMapping(value = "/excel", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value="导入excel到数据库",notes="导入excel到数据库")
	public Response importExcel(@Param("excelPath") String excelPath) throws InvalidFormatException, IOException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<PartDto> dtoList = ImportExcel.readExcel(excelPath);
		try {
			partService.importExcel(dtoList);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, null, "");
		} catch (Exception e){
			logger.error("导入失败",e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}
	
	/**
	* 纯正零部件价格同步
	* @author Administrator
	* TODO description
	* @date 2017年6月7日
	* @return
	* @throws Exception
	*/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/partPrice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="纯正零部件价格同步",notes="纯正零部件价格同步")
	public Response updatePartPrice() throws Exception{
        Map<String,Object> result = new HashMap<String,Object>();
	    try{
	        result = partService.updatePartPrice();
	        return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
	    } catch (Exception e){
            logger.error("更新失败",e);
            return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
}
